* load and clean IMO MMSI file
local infile `1' 
local outfile `2' 

import delimited using `infile' , clear

drop name v4
duplicates drop

format mmsi %10.0f // formating mmsi so it is visible

* cleaning up any imo numbers with more than 7 digits
* clearing last digit if it is zero
gen imo_div = imo/1e7
replace imo = . if imo_div < 0.1 // removes any imo numbers that don't have 7 digits

replace imo = imo/10 if imo_div>1 & imo_div<10 & mod(imo,10)==0
replace imo = imo/100 if imo_div>10 & imo_div<100 & mod(imo,100)==0
drop imo_div

* getting count by mmsi
bysort mmsi : gen cnt_mmsi = _n

reshape wide imo , i(mmsi) j(cnt_mmsi)

rename imo1 imo

* replace imo=0 with imo 2
* if there is a value for imo2 this will move it
replace imo = imo2 if imo==. & imo2~=.
replace imo2 = . if imo==imo2 

disp "`outfile'"
save `outfile', replace
